Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

function dateInput(val){
	val.daterangepicker({
		format : "YYYY-MM-DD HH:mm:ss",
		singleDatePicker : true,
		calender_style : "picker_4",
		timePicker : true, //是否显示小时和分钟
		timePickerIncrement : 1, //时间的增量，单位为分钟
	}, function(start, end, label) {
		console.log(start.toISOString(), end.toISOString(), label);
	});
}

function dateInput_s(val){
	val.daterangepicker({
		format : "YYYY-MM-DD HH:mm:ss.SSS",
		singleDatePicker : true,
		calender_style : "picker_4",
		timePicker : true, //是否显示小时和分钟
		timePickerIncrement : 1, //时间的增量，单位为分钟
	}, function(start, end, label) {
		console.log(start.toISOString(), end.toISOString(), label);
	});
}

function dateInput_d(val){
	val.daterangepicker({
		format : "YYYY-MM-DD",
		singleDatePicker : true,
		calender_style : "picker_4",
		timePicker : false, //是否显示小时和分钟
		timePickerIncrement : 1, //时间的增量，单位为分钟
	}, function(start, end, label) {
		console.log(start.toISOString(), end.toISOString(), label);
	});
}


var common = {
    //bootstrap-table用日期格式化
    dateTimeFormatter: function (value, rec, index) {
        var date = new Date(value);
        return date.Format("yyyy-MM-dd hh:mm:ss");
    },
    dateFormatter: function (value, rec, index) {
        var date = new Date(value);
        return date.Format("yyyy-MM-dd");
    },
    showDialog: function (title, msg, type,fun) {
    	BootstrapDialog.show({
    		type:type,
            title: title,
            message: msg,
            draggable:true,
            buttons: [{
                label: '关闭',
                action: function(dialogRef){
                    dialogRef.close();
                	if(fun){
                		func();
                	}
                }
            }]
        });
    },
    showLoadUrl: function (title, url) {
    	BootstrapDialog.show({
    		type:BootstrapDialog.TYPE_INFO,
            title: title,
            draggable:true,
            message: $('<div></div>').load(url)
        });
    },
    showLoadUrlForm: function (title, url,func) {
    	BootstrapDialog.show({
    		type:BootstrapDialog.TYPE_INFO,
            title: title,
            draggable:true,
            message: $('<div></div>').load(url),
            buttons: [{
                label: '取消',
                action: function(dialogRef){
                    dialogRef.close();
                }
            },
            {
                label: '提交',
                cssClass: 'btn-danger',
                action: function(dialogRef){
                    func();
                }
            }]
        });
    },
    showLoadUrlForm: function (title, url,func,w,h) {
    	BootstrapDialog.show({
    		type:BootstrapDialog.TYPE_INFO,
            title: title,
            draggable:true,
            message: $('<div></div>').load(url),
            buttons: [{
                label: '取消',
                action: function(dialogRef){
                    dialogRef.close();
                }
            },
            {
                label: '提交',
                cssClass: 'btn-danger',
                action: function(dialogRef){
                    func();
                }
            }]
        });
    },
    confirmDelete: function (func) {
    	BootstrapDialog.show({
    		type:BootstrapDialog.TYPE_INFO,
            title: "提示!",
            message: "确定要删除吗？",
            draggable:true,
            buttons: [{
                label: '取消',
                action: function(dialogRef){
                    dialogRef.close();
                }
            },
            {
                label: '确定删除',
                cssClass: 'btn-danger',
                action: function(dialogRef){
                    func();
                }
            }]
        });
    },
    showOptionSuccess: function (msg,fun) {
        msg = msg || "操作成功！";
        common.showDialog("提示！", msg,BootstrapDialog.TYPE_SUCCESS,fun);
    },
    showOptionFail: function (msg,func) {
        msg = msg || "操作失败！可能数据已经发生变化，请刷新后重新操作！";
        common.showDialog("提示！", msg,BootstrapDialog.TYPE_WARNING,func);
    },
    showServerError: function () {
        common.showDialog("提示！", "操作失败！服务器可能出错，请稍后再试！",BootstrapDialog.TYPE_WARNING);
    },
    closeBootstrapDialog: function (ms) {
    	if(ms){
    		setTimeout(function(){
    			BootstrapDialog.closeAll();
            }, ms);
    	}else{
    		BootstrapDialog.closeAll();
    	}
	},
	sendForm: function ($form, callBack) {
        var url = $form.attr("action");
        var param = $form.serialize();
        common.myPost(url, param, function (data) {
            common.myCallBack(data, callBack);
        });
    },
    myCallBack: function (data, callBack) {
        if (data && data.returnResult) {
        } else {
            if (callBack) {
                callBack(data);
            }
        }
    },
    myPost: function (url, param, callBack) {
        //$("#back",window.top.right.document).show();
        $.ajax({
            "url": url,
            "data": param,
            "success": function (data) {
                callBack(data);
            },
            "cache": false,
            "type": "POST",
            "beforSend": function () {
            },
            "complete": function () {
            },
            "error": function (xhr, error, thrown) {
                console.log(error);
                common.showServerError();
            }
        });
    }
}

window.Parsley.addValidator('mobile',
	    function (value) {
	        var phone = /^1[3|4|5|7|8][0-9]\d{8}$/;
	        return phone.test(value);
	    }, 32).addMessage('zh-cn', 'mobile', '请输入正确的手机号！');
window.Parsley.addValidator('nospecial',
	    function (value) {
	        var myReg = /[~^@\/\'\\\"#$%&\^\*\?\(\),`\+;\[\]\{\}\|\.:：<>!！￥？（），。、]/;  
	        return myReg.test(value);
	    }, 32).addMessage('zh-cn', 'nospecial', '您输入的值不可包含特殊字符');

